package com.casic.worklog.controller;

import com.casic.worklog.model.EmpSign;
import com.casic.worklog.model.Employee;
import com.casic.worklog.service.EmployeeService;
import com.casic.worklog.service.SignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/workLog")

/**
 * @author 黄志
 * @date 2019-09-13 14:08
 * @param null
 * @return 
 * @throws 
 * 
*/
public class LoginController {

    @Resource
    private EmployeeService employeeService;
    @Resource
    private SignService signService;

    @RequestMapping("/login")
    public String login ( ) {
        return "Login";
    }

    /**
     * 登录
     * 根据类型跳转不同界面
     * */
    @RequestMapping("/loginCheck.ht")
    public ModelAndView loginCheck ( HttpSession session, Employee employee,EmpSign empSign) {
        Employee employee1 = employeeService.loginCheck(employee.getUsername(), employee.getPassword());
        if (employee1==null) {
            return new ModelAndView("login", "error", "用户名或密码错误");
        } else if (employee1.getType().equals(Employee.MANAGER))  {
            session.setAttribute("loginer",employee1);
            return new ModelAndView("manager");
        }else{
            //签到次数
            empSign.setStartTimes(signService.signInTimes(employee1.getId()));
            //签退次数
            empSign.setEndTimes(signService.signOutTimes(employee1.getId()));
            //迟到次数
            empSign.setLateTimes(signService.lateTimes(employee1.getId()));
            //早退次数
            empSign.setLeaveEarlyTime(signService.leaveEarlyTimes(employee1.getId()));
            session.setAttribute("loginer",employee1);
            session.setAttribute("signMessage",empSign);
            return new ModelAndView("workNote");
        }
    }


}
